package com.bocai.dahuatech.ui.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.PointBean;
import com.bocai.dahuatech.Bean.PointListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.ui.adapter.MyPointAdapterHolder;
import com.bocai.dahuatech.ui.pressent.MyPointPressent;
import com.bocai.dahuatech.ui.view.LoadMoreRecyclerView;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.SharePrefencesUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的华积分
 */
public class MyPointActivity extends BaseActivity implements LoadMoreRecyclerView.LoadMoreListener,MyPointPressent{

    @Bind(R.id.ll_title_right)
    LinearLayout llTitle;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.rv_mypoint)
    LoadMoreRecyclerView rvMyPoint;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.tv_totalpoint)
    TextView totalPoint;
    @Bind(R.id.tv_canusepoint)
    TextView canusePoint;
    @Bind(R.id.tv_frozenpoint)
    TextView frozenPoint;
    PointBean pointBean;
    PointListBean pointlistBean;
    List<PointListBean.ContentEntity> list=new ArrayList<>();
    MyPointAdapterHolder adapter;
    @Bind(R.id.ll_mypoint_nocontent)
    LinearLayout llNoContent;
    View view;
    int page=1;
    @Override
    protected int contentView() {
        return R.layout.activity_my_point;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.mepoint);
        if(SharePrefencesUtil.getFAccessFlag(this)==2){
            llNoContent.setVisibility(View.GONE);
            llTitle.setVisibility(View.GONE);
            etSearch.setHint(R.string.orderserchhint);
            rvMyPoint.setLayoutManager(new LinearLayoutManager(this));
            rvMyPoint.setLoadMoreListener(this);
//        view=LayoutInflater.from(this).inflate(R.layout.my_point_header,null);
//        totalPoint= (TextView) view.findViewById(R.id.tv_totalpoint);
//        canusePoint= (TextView) view.findViewById(R.id.tv_canusepoint);
//        frozenPoint= (TextView) view.findViewById(R.id.tv_frozenpoint);
            getPoint();
            initEvent();
        }else{
            llNoContent.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getPoint();
            }
        });
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId== EditorInfo.IME_ACTION_SEARCH)
                {
                    page=1;
                    getPointlist(etSearch.getText().toString());
                }
                return false;
            }
        });
    }

    private void getPoint() {
        showLoading();
        getTestApi().getPoint(SharePrefencesUtil.getCustomerCode(this))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    private void getPointlist(String search) {
        getTestApi().getPointList(SharePrefencesUtil.getCustomerCode(this), search, page)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observerlist);
    }
    Observer<BaseBean> observerlist=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            swipeRefreshLayout.setRefreshing(false);
            Log.e("result", "zhang");
        }

        @Override
        public void onNext(BaseBean baseBean) {
            hideLoading();
            swipeRefreshLayout.setRefreshing(false);
            if(baseBean.getReturnNo().equals("1"))
            {

                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);
                pointlistBean=new Gson().fromJson(result,PointListBean.class);
//                setHeaderData();
                if(page==1){
                    adapter=new MyPointAdapterHolder(MyPointActivity.this,pointlistBean.getContent());
                    rvMyPoint.setAdapter(adapter);
                }else{

                    adapter.addDatas(pointlistBean.getContent());
                }

                if(pointlistBean.getContent().size()==0){
                    rvMyPoint.notifyMoreFinish(false);
                }else{
                    rvMyPoint.notifyMoreFinish(true);
                }
            }else{
                showToast(baseBean.getReturnInfo());
            }
        }
    };
    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("result","zhang");
        }

        @Override
        public void onNext(BaseBean baseBean) {
            if(baseBean.getReturnNo().equals("1")) {

                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result", result);
                pointBean=new Gson().fromJson(result,PointBean.class);
//                Double fcoin=pointBean.getContent().get(0).getFCoin();
                totalPoint.setText(getResources().getText(R.string.pointall)+pointBean.getContent().get(0).getFCoin());
                canusePoint.setText(getResources().getText(R.string.pointuse)+ pointBean.getContent().get(0).getFAvailableCoin());
                frozenPoint.setText(getResources().getText(R.string.pointfrozen)+pointBean.getContent().get(0).getFColdCoin());
                getPointlist("");
            }
        }
    };



    @Override
    public void onLoadMore() {
        page++;
        getPoint();
    }

    @Override
    public void setHeaderData() {

    }
}
